package com.gylang.print.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gylang.common.core.page.PagePlus;
import com.gylang.common.core.page.TableDataInfo;
import com.gylang.common.utils.PageUtils;
import com.gylang.print.bo.SysPrintAddBo;
import com.gylang.print.bo.SysPrintEditBo;
import com.gylang.print.bo.SysPrintQueryBo;
import com.gylang.print.domain.SysPrint;
import com.gylang.print.mapper.SysPrintMapper;
import com.gylang.print.service.ISysPrintService;
import com.gylang.print.vo.SysPrintVo;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 打印模板Service业务层处理
 *
 * @author ruoyi
 * @date 2021-06-16
 */
@Service
public class SysPrintServiceImpl extends ServiceImpl<SysPrintMapper, SysPrint> implements ISysPrintService {

    @Override
    public SysPrintVo queryById(Long id){
        return getVoById(id, SysPrintVo.class);
    }

    @Override
    public TableDataInfo<SysPrintVo> queryPageList(SysPrintQueryBo bo) {
        PagePlus<SysPrint, SysPrintVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), SysPrintVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<SysPrintVo> queryList(SysPrintQueryBo bo) {
        return listVo(buildQueryWrapper(bo), SysPrintVo.class);
    }

    private LambdaQueryWrapper<SysPrint> buildQueryWrapper(SysPrintQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysPrint> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getName()), SysPrint::getName, bo.getName());
        lqw.eq(bo.getType() != null, SysPrint::getType, bo.getType());
        lqw.eq(StrUtil.isNotBlank(bo.getTemplate()), SysPrint::getTemplate, bo.getTemplate());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(SysPrintAddBo bo) {
        SysPrint add = BeanUtil.toBean(bo, SysPrint.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(SysPrintEditBo bo) {
        SysPrint update = BeanUtil.toBean(bo, SysPrint.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysPrint entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
